import { Request, Response, NextFunction } from "express";
import jwt from "jsonwebtoken";
import { jwtSecret } from "@/configs/jwt";

interface Params {
	token: string;
}
export default async (request: Request, response: Response, next: NextFunction) => {
	try {
		const params = request.body as unknown as Params;
		jwt.verify(params.token, jwtSecret, (err, decoded) => {
			if (err) throw new Error("Token无效");
			const token = jwt.sign({ id: (decoded as any)?.id || "" }, jwtSecret as string, { expiresIn: "7d" });
			response.json({ data: { token }, message: "成功" });
		});
	} catch (error) {
		response.statusCode = 400;
		next(error);
	}
};
